package com.infodms.dms.actions.claim.other;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.Utility;
import com.infodms.dms.common.getCompanyId.GetOemcompanyId;
import com.infodms.dms.dao.claim.other.BonusDAO;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TmDealerPO;
import com.infodms.dms.po.TmFineNewsPO;
import com.infodms.dms.po.TtAsWrFinePO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

/**
 * 
* @ClassName: Bonus 
* @Description: TODO(正负激励) 
* @author wangchao 
* @date Jun 16, 2010 4:19:54 PM 
*
 */
public class Bonus {
	private Logger logger = Logger.getLogger(Bonus.class);
	private final BonusDAO dao = BonusDAO.getInstance();
	private final String BONUS_URL = "/jsp/claim/other/bonus.jsp";// 主页面（查询）
	private final String ENCOURAGE_URL = "/jsp/claim/other/encourage.jsp"; //处罚页面
	private final String PUNISH_HISTORY_URL = "/jsp/claim/other/punishHistory.jsp"; //查询 处罚历史
	private final String NEW_LIST_URL = "/jsp/claim/other/newsList.jsp"; //首页新闻列表
	private final String BONUS_DLR_URL = "/jsp/claim/other/bonusDlr.jsp";// 经销商主页面（查询）
	private final String BONUS_DLR_DETAIL = "/jsp/claim/other/encourageDetailDlr.jsp";// 经销商主页面（查询）
	/**
	 * 
	* @Title: bonusForward 
	* @Description: TODO(正负激励跳转) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void bonusForward() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			act.setForword(BONUS_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: punishForward 
	* @Description: TODO(处罚页面跳转) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void punishForward() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			RequestWrapper request = act.getRequest();
			String dealerId = request.getParamValue("DEALER_ID");
			TmDealerPO dealer = new TmDealerPO();
			dealer.setDealerId(Long.valueOf(dealerId));
			TmDealerPO dealerValue = (TmDealerPO) dao.select(dealer).get(0);
			
			//取得该用户拥有的产地权限
			String yieldly = CommonUtils.findYieldlyByPoseId(logonUser.getPoseId());
			
			act.setOutData("yieldly", yieldly);
			act.setOutData("DEALER_ID", dealerId);
			act.setOutData("encourageMan", logonUser.getName());
			act.setOutData("dealerName", dealerValue.getDealerName());
			act.setOutData("dealerCode", dealerValue.getDealerShortname());
			act.setOutData("encourageDate", sdf.format(new Date()));
			act.setForword(ENCOURAGE_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: punishHistoryForward 
	* @Description: TODO(处罚历史跳转) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void punishHistoryForward() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String dealerId = request.getParamValue("DEALER_ID");

			act.setOutData("DEALER_ID", dealerId);
			act.setForword(PUNISH_HISTORY_URL);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	/**
	 * 
	* @Title: queryDealer 
	* @Description: TODO(查询经销商) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void queryDealer () {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage"))
					: 1; // 处理当前页
			//从session中取得车厂公司id		
			Long oemCompanyId = GetOemcompanyId.getOemCompanyId(logonUser);		
			String dealerCode = request.getParamValue("DEALER_CODE");
			String dealerName = request.getParamValue("DEALER_NAME");
			Map<String,String> param = new HashMap();
			param.put("dealerCode", dealerCode);
			param.put("dealerName", dealerName);
			param.put("oemCompanyId", String.valueOf(oemCompanyId));
			PageResult<TmDealerPO> ps =  dao.queryDealer(param, Constant.PAGE_SIZE, curPage);
			act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: punish 
	* @Description: TODO(罚款处理) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void punish () {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		Long createBy = logonUser.getUserId();
		
		try {
			RequestWrapper request = act.getRequest();
			@SuppressWarnings("unused")
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage"))
					: 1; // 处理当前页
			String dealerId = request.getParamValue("DEALER_ID"); //经销商ID
			String fineSum = request.getParamValue("FINE_SUM"); //罚款金额
			String fineReason = request.getParamValue("FINE_REASON"); //罚款原因
			String remark = request.getParamValue("REMARK"); //备注
			String yieldly = request.getParamValue("yieldly");//奖励产地
			
			TtAsWrFinePO tawfp = new TtAsWrFinePO();
			tawfp.setFineId(Utility.getLong(SequenceManager.getSequence("")));
			tawfp.setDealerId(Utility.getLong(dealerId));
			tawfp.setFineSum(Utility.getDouble(fineSum));
			tawfp.setFineReason(fineReason);
			tawfp.setPayStatus(Utility.getInt(Constant.PAY_STATUS_01));
			tawfp.setPayDate(new Date());
			tawfp.setRemark(remark);
			tawfp.setCreateBy(createBy);
			tawfp.setCreateDate(new Date());
			tawfp.setYieldly(Integer.parseInt(yieldly));
			dao.insert(tawfp);
			String[] newsIds = request.getParamValues("newsId");
			if(newsIds!=null){
				for(int i = 0; i < newsIds.length; i++){
					TmFineNewsPO tfn = new TmFineNewsPO();
					tfn.setFineNewsId(Utility.getLong(SequenceManager.getSequence("")));
					tfn.setFineId(tawfp.getFineId());
					tfn.setNewsId(Long.valueOf(newsIds[i]));
					tfn.setCreateBy(logonUser.getUserId());
					tfn.setCreateDate(new Date());
					dao.insert(tfn);
				}
			}
			
			bonusForward();
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: punishHistory 
	* @Description: TODO(查询罚款历史) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void punishHistory() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String deductStartDate = CommonUtils.checkNull(request.getParamValue("deductStartDate"));
			String deductEndDate = CommonUtils.checkNull(request.getParamValue("deductEndDate"));
			String status = CommonUtils.checkNull(request.getParamValue("status"));
			String yieldly = CommonUtils.checkNull(request.getParamValue("yieldly"));
			Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage"))
					: 1; // 处理当前页 
			String dealerId = request.getParamValue("DEALER_ID");
			TtAsWrFinePO twafp = new TtAsWrFinePO();
			twafp.setDealerId(Utility.getLong(dealerId));
			PageResult<Map<String,Object>> ps =  dao.punishQuery(dealerId,deductStartDate,deductEndDate,status,yieldly, 10, curPage);
			act.setOutData("ps", ps);
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	
	/**
	 * 
	* @Title: punishHistory 
	* @Description: TODO(新闻查询) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	public void newsQuery() {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String COMMAND = CommonUtils.checkNull(request.getParamValue("COMMAND"));
			if(!"".equals(COMMAND)){
				String newId = CommonUtils.checkNull(request.getParamValue("newId"));
				Map<String,Object> map = dao.queryNewsById(newId);
				act.setOutData("map", map);
			}else{
				act.setForword(NEW_LIST_URL);
			}
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: queryDealer 
	* @Description: TODO(经销商查询正负激励) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void queryDealerByDlr () {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		RequestWrapper request = act.getRequest();
		try {
			String COMMAND = CommonUtils.checkNull(request.getParamValue("COMMAND"));
			if(!"".equals(COMMAND)){
				String deductStartDate = CommonUtils.checkNull(request.getParamValue("deductStartDate"));
				String deductEndDate = CommonUtils.checkNull(request.getParamValue("deductEndDate"));
				String status = CommonUtils.checkNull(request.getParamValue("status"));
				String yieldly = CommonUtils.checkNull(request.getParamValue("yieldly"));
				Integer curPage = request.getParamValue("curPage") != null ? Integer
					.parseInt(request.getParamValue("curPage"))
					: 1; // 处理当前页
				//从session中取得车厂公司id		
				Long oemCompanyId = GetOemcompanyId.getOemCompanyId(logonUser);		
				String dealerCode = request.getParamValue("DEALER_CODE");
				String dealerName = request.getParamValue("DEALER_NAME");
				Map<String,String> param = new HashMap();
				param.put("dealerId", logonUser.getDealerId());
//				param.put("dealerCode", dealerCode);
//				param.put("dealerName", dealerName);
//				param.put("oemCompanyId", String.valueOf(oemCompanyId));
				PageResult<Map<String,Object>> ps =  
					dao.punishQuery(logonUser.getDealerId(),deductStartDate,deductEndDate,status,yieldly, 10, curPage);
				act.setOutData("ps", ps);
			}else{
				act.setForword(BONUS_DLR_URL);
			}
			
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
	/**
	 * 
	* @Title: queryDealer 
	* @Description: TODO(经销商查询正负激励明细) 
	* @param     设定文件 
	* @return void    返回类型 
	* @throws
	 */
	@SuppressWarnings("unchecked")
	public void queryDealerByDlrDetail () {
		ActionContext act = ActionContext.getContext();
		AclUserBean logonUser = (AclUserBean) act.getSession().get(
				Constant.LOGON_USER);
		RequestWrapper request = act.getRequest();
		try {
			String fineId = CommonUtils.checkId(request.getParamValue("fineId"));
			Map<String,Object> mapEncourage = dao.dlrQueryEncourageDetail(fineId);
			List<Map<String,Object>> listNews = dao.dlrQueryEncourageNewsDetail(fineId);
			
			act.setOutData("mapEncourage", mapEncourage);
			act.setOutData("listNews", listNews);
			act.setForword(BONUS_DLR_DETAIL);
			
		} catch (Exception e) {
			BizException e1 = new BizException(act, e,
					ErrorCodeConstant.QUERY_FAILURE_CODE, "索赔基本参数设定");
			logger.error(logonUser, e1);
			act.setException(e1);
		}
	}
}
